<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link rel="stylesheet" type="text/css" th:href="@{/assets/css/layui.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/assets/css/view.css}"/>
    <title>用户修改密码</title>
</head>
<body class="layui-view-body">
<div class="layui-content">
    <div class="layui-row">
        <div class="layui-card">
            <div class="layui-card-header">用户修改密码</div>
            <form id="userForm" class="layui-form layui-card-body">
                <input type="hidden" name="id" th:value="${sysUser.id}">
                <div class="layui-form-item">
                    <label class="layui-form-label">登录名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="username" th:value="${sysUser.username}" th:readonly="true"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">新密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="password" id="password" required lay-verify="password"
                               placeholder="新密码" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">确认密码</label>
                    <div class="layui-input-inline">
                        <input type="password" name="confirmPwd" required lay-verify="confirmPwd" placeholder="再次输入密码"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="button" class="layui-btn layui-btn-blue" lay-submit lay-filter="save"
                               th:value="保存"></input>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script th:src="@{/assets/layui.all.js}"></script>
<script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
<script th:inline="javascript">
    /*<![CDATA[*/
    var contextPath = [[${#request.getContextPath()}]]; //获取当前应用路径
    var userId = [[${sysUser.id}]];
    /*]]>*/
</script>
<script type="text/javascript">
    layui.use(['form'], function () {
        var form = layui.form
            , layer = layui.layer;

        //添加验证规则
        form.verify({
            password: function (value, item) {
                if (!new RegExp("^[\\S]{6,12}$").test(value)) {
                    return "密码必须6到12位，且不能出现空格";
                }
            },
            confirmPwd: function (value, item) {
                var pwd = $('input[name=password]').val();
                if (pwd != value)
                    return "两次输入密码不一致，请重新输入！";
            }
        });

        //监听提交
        form.on('submit(save)', function (data) {
            var data = $("#userForm").serialize();
            $.ajax({
                url: contextPath + "/a/user/modifySubmit",
                type: "post",
                data: data,
                success: function (msg) {
                    if (msg == 'success') {
                        layer.msg("修改成功", {
                            time: 1000
                        }, function () {
                            //在iframe中 关闭当前弹出层
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                    } else {
                        layer.alert("修改失败");
                    }
                }
            });
        })
    });

</script>
</body>
</html>